package cn.iocoder.yudao.framework.mybatis.component;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Pair;
import cn.iocoder.yudao.framework.datasource.utils.SnowIdUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @className: MinPartitionDateComponent
 * @author: tangkh
 * @date: 2023-09-19 16:35
 */
public class MinPartitionDateComponent {
    private static final Logger log = LoggerFactory.getLogger(MinPartitionDateComponent.class);
    private static final String MIN_VAA1_CREATED_TIME_PRE = "MIN_VAA1_CREATED_TIME_PRE_";
    private static final String MIN_VAC1_CREATED_TIME_PRE = "MIN_VAC1_CREATED_TIME_PRE_";
    private static final String MIN_VAE1_CREATED_TIME_PRE = "MIN_VAE1_CREATED_TIME_PRE_";
    public static final Long CACHE_TIME = 300L;
    //@Autowired
    // private RedisTemplate<String, Object> redisTemplate;
    //获取业务的sdk

    public MinPartitionDateComponent() {
    }


    public String getBusinessMinPartitionDate(String ids) {
        //先获取业务表种最小创建时间如果获取不到，则计算ids的最小时间
        // todo getMinData
        return null;
    }

    private static String getDateYesterday(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(5, -1);
        String format = DateUtil.format(calendar.getTime(), DatePattern.NORM_DATETIME_FORMAT);
        return format;
    }

    private Pair<Date, List<Long>> getMinData(String idsStr, String prefix) {
        if (StringUtils.isBlank(idsStr)) {
            return null;
        } else {
            List<Long> ids = (List) Arrays.asList(idsStr.split(",")).stream().map((id) -> {
                return Long.valueOf(id);
            }).collect(Collectors.toList());
            Pair<Date, List<Long>> llp = SnowIdUtil.minDateNoSnowIds(ids);
            List<Long> noSnowIs = (List) llp.getValue();
            Date minDate = llp.getKey();
            if (CollectionUtil.isNotEmpty(noSnowIs)) {
                Long redisValue = null; //GlobalVariable.LongIsNull(this.redisTemplate.opsForValue().get(getCombinationKey(prefix, noSnowIs)));
                if (redisValue > 0L) {
                    log.info("命中缓存 : key : {}, value :{} ", getCombinationKey(prefix, noSnowIs), redisValue);
                    this.setRedisValue(new Date(redisValue), noSnowIs, prefix);
                    Date redisDate = new Date(redisValue);
                    Date compareMinDate = Optional.ofNullable(minDate).map((min) -> min.compareTo(redisDate) > 0 ? redisDate : min).orElse(redisDate);
                    return Pair.of(compareMinDate, ids);
                }
            }
            return llp;
        }
    }

    private void setRedisValue(Date time, List<Long> noSnowIs, String prefix) {
        String combinationKey = getCombinationKey(prefix, noSnowIs);
        // this.redisTemplate.opsForValue().set(combinationKey, time.getTime(), CACHE_TIME, TimeUnit.SECONDS);
    }

    public static String getCombinationKey(String prefix, List<Long> noSnowIs) {
        List<Long> collect = noSnowIs.stream().sorted((o1, o2) -> o1 < o2 ? 0 : 1).collect(Collectors.toList());
        return prefix + collect.stream().map(String::valueOf).collect(Collectors.joining("_"));
    }
}
